package com.yedean.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yedean.mall.dto.PmsProductCategoryWithChildrenItem;
import com.yedean.mall.entity.PmsProductCategory;
import com.yedean.mall.entity.PmsProductCategoryAttributeRelation;
import com.yedean.mall.mapper.PmsProductCategoryAttributeRelationMapper;
import com.yedean.mall.mapper.PmsProductCategoryMapper;
import com.yedean.mall.service.IPmsProductCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author yedean
 * @since 2022-08-09
 */
@Service
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> implements IPmsProductCategoryService {

    @Resource
    PmsProductCategoryAttributeRelationMapper relationMapper;


    @Override
    public void create(PmsProductCategory pmsProductCategory, List<Long> productAttributeIdList) {
        pmsProductCategory.setProductCount(0);
        //没有父分类时为一级分类
        setCategoryLevel(pmsProductCategory);
        baseMapper.insert(pmsProductCategory);
        //如果有属性关联
        if (!CollectionUtils.isEmpty(productAttributeIdList)) {
            insertRelationList(pmsProductCategory.getId(), productAttributeIdList);
        }
    }

    @Override
    public IPage<PmsProductCategory> getList(Long parentId, Integer pageSize, Integer pageNum) {
        Page<PmsProductCategory> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getParentId, parentId);

        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public void updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsProductCategory category = new PmsProductCategory();
        category.setShowStatus(showStatus);

        UpdateWrapper<PmsProductCategory> wrapper = new UpdateWrapper<>();
        wrapper.in("id", ids);

        baseMapper.update(category, wrapper);
    }

    @Override
    public void updateNavStatus(List<Long> ids, Integer navStatus) {
        PmsProductCategory category = new PmsProductCategory();
        category.setNavStatus(navStatus);

        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProductCategory::getId, ids);

        baseMapper.update(category, wrapper);

    }

    @Override
    public List<PmsProductCategoryWithChildrenItem> listWithChildren() {
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getParentId, 0);

        List<PmsProductCategory> parentCategoryList = baseMapper.selectList(wrapper);
        wrapper.clear();
        return parentCategoryList.stream()
                .map(pc -> {
                    PmsProductCategoryWithChildrenItem item = new PmsProductCategoryWithChildrenItem();
                    item.setParent(pc);
                    return item;
                }).peek((item) -> {
                    wrapper.eq(PmsProductCategory::getParentId, item.getParent().getId());
                    List<PmsProductCategory> childList = baseMapper.selectList(wrapper);
                    item.setChildren(childList);
                }).collect(Collectors.toList());
    }


    /**
     * 批量插入商品分类与筛选属性关系表
     *
     * @param productCategoryId      商品分类id
     * @param productAttributeIdList 相关商品筛选属性id集合
     */
    private void insertRelationList(Long productCategoryId, List<Long> productAttributeIdList) {
        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
        for (Long productAttrId : productAttributeIdList) {
            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
            relation.setProductAttributeId(productAttrId);
            relation.setProductCategoryId(productCategoryId);
            relationList.add(relation);
        }
        relationList.forEach(relationMapper::insert);
    }


    /**
     * 根据分类的parentId设置分类的level
     */
    private void setCategoryLevel(PmsProductCategory productCategory) {
        //没有父分类时为一级分类
        if (productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        } else {
            //有父分类时选择根据父分类level设置
            PmsProductCategory parentCategory = baseMapper.selectById(productCategory.getParentId());
            if (parentCategory != null) {
                productCategory.setLevel(parentCategory.getLevel() + 1);
            } else {
                productCategory.setLevel(0);
            }
        }
    }
}
